<template>
    <div class="zixun">
      <PublicHead></PublicHead>
      <div class="ruzhu">
        <div class="ruzhuBox">
  
          <div class="title">
            <div>实名投诉</div>
            <div>/&ensp;SHIMINGTOUSU</div>
          </div>
          <div class="title2">
            【我要投诉】
          </div>
          <div class="title3">
            (注：提交内容必须如实反映相关情况，遵守国家法律法规，违者将依法追究相关责任)
          </div>
          <div class="ruzhuForm">
            <el-form ref="form" label-position="top" :model="formData" :rules="rules">
              <el-row>
                <el-col :span="12" class="form-item">
                  <el-form-item label="主题" prop="messageSubject" >
                    <el-input v-model="formData.messageSubject" ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12" class="form-item">
                  <el-form-item label="姓名" prop="contactName" >
                    <el-input v-model="formData.contactName" ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
             
              <el-row>
                <el-col :span="12" class="form-item">
                  <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="formData.phone" ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12" class="form-item">
                  <el-form-item label="验证码" prop="code" required>
                      <el-input v-model="formData.code">
                    <el-button class="yzm" slot="suffix" @click="sendCode" :disabled="disabled">{{ yzmshow()}}</el-button>
                       </el-input>
                      </el-form-item>
  
                </el-col>
              </el-row>
             
              <el-row>
                <el-col :span="24" class="form-item">
                  <el-form-item label="留言内容" prop="leaveMessage">
                    <el-input type="textarea" :rows="6"  v-model="formData.leaveMessage">
                    </el-input>
                  </el-form-item>
                </el-col>
  
              </el-row>
            </el-form>
          </div>
          <div>
          </div>
          <div class="submit">
                      <button @click="submit">我要提交</button>
                  </div>
        </div>
        <div class="ruzhuBox2">
          <div class="title2">
              【全部回复】
          </div>
            <el-table :data="tableData" class="my-table" border style="width: 100%;" @row-click="todetail">
              <el-table-column width="800px" align="center" label="信件标题" prop="messageSubject"
                :label-class-name="'table-header'" >
                <template slot-scope="scope">
                <span class="name">{{ scope.row.messageSubject }}</span>
              </template>
              </el-table-column>
              <el-table-column  align="center" label="提交时间" prop="gmtCreate"
                :label-class-name="'table-header'">
              </el-table-column>
              <el-table-column width="150" align="center" label="审核状态" prop="messageStatus" :label-class-name="'table-header'">
                <template slot-scope="scope">
                  <span class="status" v-if="scope.row.messageStatus=='0'">待回复</span>
                  <span class="status" v-if="scope.row.messageStatus=='1'">已回复</span>
                </template>
              </el-table-column>
            </el-table>
            <div class="pages">
          <el-button @click="pervious">上一页</el-button>
          <el-pagination  @current-change="handleCurrentChange"
        :current-page="page"  :page-size="size" background
        layout=" pager" :total="total"  ref="pageGroup"></el-pagination>
        <el-button @click="next">下一页</el-button>
        <span>共{{ Math.ceil( total/size) }}页</span>
        </div>
        </div>
        
      </div>
      <PublicFoot></PublicFoot>
    </div>
  </template>
  
  <script>
  import PublicHead from '../components/PublicHead.vue'
  import PublicFoot from '../components/PublicFoot.vue'
  import { message_list ,message_send,message_add} from '@/api/api';
  export default {
    data() {
      return {
        formData: {
          userId: this.$store.state.userId,
          code: "",
          phone: "",
          contactName: "",
          messageSubject: "",
          messageType: 2,
          leaveMessage: "",
        },
      disabled: false, // 按钮是否可用
      countDown: 60 ,// 倒计时时间（单位：秒）
        page:1,
        size:4,
        total:0,
        tableData: [],
        rules: {
              code: [
                      { required: true, message: '请输入验证码', trigger: 'blur' },
                  ],
                  phone: [
                      { required: true, message: '请输入联系人手机号', trigger: 'blur' },
                      { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
                  ],
                  contactName: [
                      { required: true, message: '请输入联系人姓名', trigger: 'blur' },
                  ],
                  messageSubject: [
                      { required: true, message: '请输入消息主题', trigger: 'blur' },
                  ],
                  messageType: [
                      { required: true, message: '请选择消息类型', trigger: 'change' },
                  ],
                  leaveMessage: [
                      { required: true, message: '请输入消息内容', trigger: 'blur' },
                  ],
              }
      };
    },
    methods:{
      getdata(){
        message_list({pageNum:this.page,pageSize:'4',messageType:2}).then((res)=>{
          this.total=res.data.data.total
          this.tableData=[...res.data.data.records]
          console.log(this.tableData);
          this.tableData = res.data.data.records.map(item => ({
                     ...item,
                     gmtCreate: new Date(item.gmtCreate).toISOString().slice(0, 10)
               }));
      })
      },
      todetail(row){
        localStorage.setItem('zixunId',row.id)
        this.$router.push('/zixundetail');
      },
      yzmshow(){
        return this.disabled==false?'获取验证码':this.countDown+'秒后重试'
      },
      sendCode(){
        message_send({phone:this.formData.phone}).then((res)=>{
          console.log(res);
          if(this.formData.phone){
            this.$message({
            message: '短信发送成功',
            type: 'success',
          });
          this.disabled = true; // 禁用按钮
          let timer = setInterval(() => {
            if (this.countDown > 0) {
              this.countDown--;
              console.log(666);
        } else {
          clearInterval(timer);
          this.disabled = false; // 启用按钮
          this.countDown = 60; // 重置倒计时时间
        }
      }, 1000);
          }else{
          this.$message.error('请先输入手机号！');
          }
        })
      },
      submit(){
        console.log(this.formData);
        message_add(this.formData).then((res)=>{
          if(res.data.state=='20000'){
            this.$message({
            message: '投诉提交成功',
            type: 'success',
          });
          this.$router.push('/homepage');
          }else{
            this.$message.error(res.data.message);
          }
         
        })
      },
       //分页组件
       handleCurrentChange(currentPage) {
        this.page = currentPage
        this.getdata()
      },
      pervious() {
        this.$refs.pageGroup.prev()
      },
      next() {
        this.$refs.pageGroup.next()
      },
    },
    mounted(){
      this.getdata()
     
    },
    components: {
      PublicHead,
      PublicFoot,
    }
  }
  </script>
  
  <style scoped lang="scss">
  .pages{
    display: flex;
    align-items: center;
    position: absolute;
    bottom: 5%;
    left: 50%;
    transform: translateX(-50%);
    span{
      margin-left: 10px;
      font-size: 14px;
    }
  }
  .yzm {
      background: #0371E5;
      font-family: Microsoft YaHei;
      color: #FFFFFF;
  }
  .el-input {
      width: 400px;
  }
  .submit {
      width: 100%;
      display: flex;
      justify-content: center;
      margin: 20px;
      button {
        cursor: pointer;
          width: 98px;
          height: 40px;
          background: #2797E8;
          border-radius: 3px 3px 3px 3px;
          border: 0;
          font-family: Microsoft YaHei;
          font-weight: bold;
          color: #FFFFFF;
      }
  }
   .name {
     width: 208px;
     height: 45px;
     font-size: 16px;
     font-family: Microsoft YaHei;
     font-weight: bold;
     color: #FD8544;
     float: left;
     line-height: 45px;
   }
  
  .ruzhuForm{
    padding: 20px 50px;
  }
  .ruzhu {
    width: 100%;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
  }
  .ruzhuBox2{
    width: 1220px;
  height: 480px;
  background: #FFFFFF;
  box-sizing: border-box;
  padding: 10px 50px;
  margin: 30px 0px;
  position: relative;
  .pagination{
  position: absolute;
  transform: translateX(-50%);
  left: 50%;
  bottom: 5%;
  }
  .title2 {
      width: 150px;
      height: 40px;
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #1285D7;
      margin-top: 30px;
    }
  }
  .ruzhuBox {
    width: 1220px;
    height: 100%;
    background: #FFFFFF;
    margin: 0 auto;
    box-sizing: border-box;
    padding: 10px 50px;
    margin: 30px;
    .title {
      display: flex;
  
      div:nth-of-type(1) {
        width: 80px;
        height: 50px;
        font-size: 18px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        color: #262626;
        line-height: 50px;
        margin-bottom: 20px;
        border-bottom: 4px #1285D7 solid;
  
      }
  
      div:nth-of-type(2) {
        height: 50px;
        font-size: 15px;
        font-family: Arial;
        font-weight: 400;
        color: #DCDCDC;
        line-height: 53px;
        margin-right: 5px;
  
        border-bottom: 4px #1285D7 solid;
  
      }
  
      div:nth-of-type(3) {
        width: 189px;
        height: 50px;
        font-size: 15px;
        font-family: Arial;
        font-weight: 400;
        color: #DCDCDC;
        line-height: 50px;
  
        border-bottom: 4px #1285D7 solid;
  
      }
  
    }
  
    .title2 {
      width: 150px;
      height: 40px;
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #1285D7;
    }
    .title3 {
      width: 1136px;
  height: 51px;
  background: #FDF1F1;
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #C70500;
  text-align: center;
  line-height: 51px;
    }
  }
  
  .name {
    width: 208px;
    height: 45px;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #FD8544;
  }
  ::v-deep .el-input__suffix{
    right: 0px;
  }
  .text,
  .iconDownload {
    height: 14px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #1285D7;
  }
  
  .el-table__header {
    background-color: #1285D7 !important;
    color: white;
  }
  
  ::v-deep .el-table__header th.is-leaf,
  ::v-deep .el-table__header th.gutter {
    background-color: #1285D7 !important;
    color: white;
    font-size: 16px;
  }
  
  .status {
    width: 48px;
    height: 16px;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #D62502;
  }
  
  .el-button--primary {
    background-color: #fd8544;
    border-color: #fd8544;
  }
  </style>